# coding=utf-8
import datetime
from application import app,db
'''
python manager.py runjob -m order/CancelOrder
'''
class JobTask():
    def __init__(self):
        pass

    def run(self, params):
        self.__cancel_order()
        return



    def __cancel_order(self):
        # now_time = get_now_time()
        # now_time_str = date_format(now_time)
        # db.session.query(Order, OrderItem)\
        #     .join(OrderItem, OrderItem.order_id==Order.id)\
        #     .filter(Order.status==0, Order.close_pay_time < now_time )\
        #     .update({
        #             'Order.status' : 0,
        #             'Order.updated_time' : now_time_str,
        #             'Order.end_time' : now_time_str,
        #             'OrderItem.status': 0,
        #             'OrderItem.updated_time': now_time_str,
        #             'OrderItem.end_time': now_time_str,
        #         })
        try :
            # order_query = Order.query.filter(Order.status==1, Order.close_pay_time < now_time )
            # order_list = order_query.all()
            # order_id_list = list( order.id for order in order_list )
            # if order_id_list :
            #     order_item_query = OrderItem.query.filter( OrderItem.order_id.in_(order_id_list))
            #     order_query.update({
            #             'status' : 0,
            #             'updated_time' : now_time_str,
            #             'end_time': now_time_str
            #          },
            #         synchronize_session=False)
            #     order_item_query.update({
            #              'status' : 0,
            #              'updated_time' : now_time_str,
            #              'end_time': now_time_str
            #          },
            #         synchronize_session=False)
                db.session.commit()
        except Exception as e:
            print(e)
            db.session.rollback()





        # if order_info.close_pay_time < get_now_time():
        #     try :
        #         info = order_info
        #         info.status = 0
        #         info.update_time = get_current_time()
        #         info.end_time = get_current_time()
        #         db.session.add(info)
        #         db.session.flush()
        #         order_item_list = OrderItem.query.filter_by(order_id=order_info.id).order_by(OrderItem.id.asc()).all()
        #         for item in order_item_list :
        #             item_info = item
        #             item_info.status = order_info.status
        #             item_info.updated_time = get_current_time()
        #             item_info.end_time = get_current_time()
        #             db.session.add(item_info)
        #             db.session.flush()
        #         db.session.commit()
        #         return {"error_msg": "订单已经过期"}
        #     except:
        #         db.session.rollback()
        #         return {"error_msg": "服务器异常，请重试"}






